<%@ page import="com.lovebridge.model.User" %>
<%@ page import="com.lovebridge.SystemManager" %>
<%@ page import="java.sql.Date" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" errorPage="/error.jsp" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%
    String userId = request.getParameter("id");
    String password = request.getParameter("password");
    String email = request.getParameter("email");
    String role = request.getParameter("role");
    User user = SystemManager.getPersist().read(User.class, "select * from user where id=?", Integer.parseInt(userId));
    if (user != null) {
        if (request.getParameter("editUser") != null) {
            if (email != null) user.setEmail(email);
            if (password != null) user.setPassword(password);
            if (role != null) user.setRole(Integer.parseInt(role));
            user.setUpdateTime(new Date(System.currentTimeMillis()));
            SystemManager.getPersist().update(user);
            request.setAttribute("message", "成功保存用户信息。");
            request.getRequestDispatcher("users.jsp").forward(request, response);
        }
    } else {
        request.setAttribute("message", "没有匹配用户！");
        request.getRequestDispatcher("users.jsp").forward(request, response);
    }
%>
<jsp:include page="/header.jsp" flush="true"/>
<form id="editUser" name="editUser"
      action="editUser.jsp" method="post">
    <input type="hidden" name="id" value="<%=user.getId()%>"/>
    <input type="hidden" name="editUser" value="true"/>

    <div class="search">
        <ul>
            <li class="title">
                编辑用户
            </li>
            <li>
                <c:if test="${!empty message}">
                    <font color="RED"><c:url value="${message}"/> </font>
                </c:if>
            </li>
            <li>
                <b>用户名: </b>
                <input type="text" readonly="true" name="userName"
                       id="userName" value="<%=user.getUserName()%>"/>
            </li>
            <li>
                <b>密码: </b>
                <input type="password" value="" name="password" id="password"/>
            </li>
            <li>
                <b>Email: </b>
                <input type="text" name="email"
                       id="email" value="<%=user.getEmail()%>"/>
            </li>
            <li>
                <b>角色: </b>
                <select name="role">
                    <option value="1" <%=user.getRole() == 1 ? "selected" : ""%>>管理员</option>
                    <option value="2" <%=user.getRole() == 2 ? "selected" : ""%>>">用户</option>
                </select>
            </li>
            <li>
                <input type="submit" value="提交"/>
            </li>
        </ul>
    </div>
</form>
<jsp:include page="/footer.jsp" flush="false"/>
